require("../util.js");
require("../component/components.js");
// require("../component/purchaseHeader.js");
// require("../component/footer.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#goodsDetails",
    data:{
        isShow:1,
        swipeY:"",
        isNum:1,
        isClassify:false,
        number:1,
        productId:"",
        type:"",//1 正常  2 折扣   3 秒杀  4 爆款
        productById:{},
        comment:{},
        shop:{},
        pageNumber:1,
        isTop:false,
        commentById:{},
        commentById1:[],
        commentById2:[],
        hours:"",//时
        minutes:"",//分
        seconds:"",//秒
        endDate:"",
        endSecond:"",
        num:"",
        users:[],
        uid:"",
        cartCountNum:"",//购物车数量
        colorName:"",//颜色
        colorId:"",
        specName:"",//规格
        specId:"",
        integral:"",//价格
        imgSrc:"",
        skuId:"",
        stocks:"",  //库存
        isShare:false,
        specIdList:[],
        colorIdList:[],
        isBuyNow:""  //[0-立即购买 1加入购物车]
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.productId=this.searchObject&&this.searchObject.productId;
        this.type=this.searchObject&&this.searchObject.type;
        console.log(this.searchObject&&this.searchObject.loginUid);
        // if(this.searchObject&&this.searchObject.loginUid){
        //     this.uid=this.searchObject&&this.searchObject.loginUid;
        // }else{
        //     if(userInfo){
        //         this.uid=userInfo.loginUid;
        //     }else{
        //         location.href="/loginIndex"
        //     }
        // }
        this.isNum=this.type;
        if(this.type==1){
            this.getProductById();
        }
       /* else if(this.type==2){
            this.getProductDiscountsById();
        }else if(this.type==3){
            this.getSeckillById();
        }else if(this.type==4){
            this.getLuckyBurstById();
        }*/
        //this.cartCount();
        //this.getProductCommentById();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        // getGoodsTab:function(num){
        //     this.isShow=num;
        //     // if(this.isShow==1){
        //     //     this.getProductById();
        //     // }else if(this.isShow==3){
        //     //     console.log("dddd")
        //     //     this.getProductCommentById()
        //     // }
        //     window.scrollTo(0,0);
        // },
        //显示弹框
        isDisplay:function(){
            this.isClassify=!this.isClassify;
        },
        //数量减
        getReduce:function(){
            if(this.number==0){
                return false;
            }else{
                this.number--;
            }
        },
        //数量加
        getAdd:function(){
            if(this.number<this.stocks){
                this.number++;
            }
        },
        //购物车数量
        cartCount:function(){
            var that=this;
            this.ajax({url:"/purchase/cartCount",data:{loginUid:that.uid},dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.cartCountNum=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取购物车数量失败！")
                }
            });
        },
        //乐购商品详情
        getProductById:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                productId:that.productId
            };
            this.ajax({url:"/purchase/getProductById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productById=b.json.data;
                    that.integral=that.productById.integral;
                    that.imgSrc=that.productById.images[0].imgSrc;
                    that.comment=that.productById.comment;
                    that.shop=that.productById.shop;
                    console.log(that.productById)
                }else{
                    vm.$refs.prompt.toast("获取商品详情失败！")
                }
            });
        },
       /* //乐购爆款商品详情
        getLuckyBurstById:function(){
            var that=this;
            this.ajax({url:"/purchase/getLuckyBurstById",data:{productId:that.productId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productById=b.json.data;
                    that.integral=that.productById.integral;
                    that.imgSrc=that.productById.images[0].imgSrc;
                    that.comment=that.productById.comment;
                    that.endDate=that.productById.endDate;
                    that.endSecond=parseInt(that.endDate)*60*60;
                    that.compareDate();
                    that.num=that.productById.users.length;
                    that.users=that.productById.users;
                }else{
                    vm.$refs.prompt.toast("获取爆款商品详情失败！")
                }
            });
        },
        //乐购折扣商品详情
        getProductDiscountsById:function(){
            var that=this;
            this.ajax({url:"/purchase/getProductDiscountsById",data:{productId:that.productId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productById=b.json.data;
                    that.integral=that.productById.activityPrice;
                    that.imgSrc=that.productById.images[0].imgSrc;
                    that.comment=that.productById.comment;
                    that.endDate=that.productById.endDate;
                    that.endSecond=parseInt(that.endDate)*60*60;
                    that.compareDate();
                    console.log(that.productById)
                }else{
                    vm.$refs.prompt.toast("获取折扣商品详情失败！")
                }
            });
        },
        //乐购秒杀商品详情
        getSeckillById:function(){
            var that=this;
            this.ajax({url:"/purchase/getSeckillById",data:{productId:that.productId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productById=b.json.data;
                    that.integral=that.productById.activityPrice;
                    that.imgSrc=that.productById.images[0].imgSrc;
                    that.comment=that.productById.comment;
                    that.endDate=that.productById.endDate;
                    that.endSecond=parseInt(that.endDate)*60*60;
                    that.compareDate();
                    that.num=that.productById.users.length;
                    that.users=that.productById.users;
                }else{
                    vm.$refs.prompt.toast("获取秒杀商品详情失败！")
                }
            });
        },*/
        //乐购商品评价
        // getProductCommentById:function(){
        //     var that=this;
        //     var data={
        //         productId:that.productId,
        //         pageNumber:that.pageNumber
        //     };
        //     this.ajax({url:"/purchase/getProductCommentById",data:data,dataType:"json",method:"post"},function(b){
        //         if(b.json.code==0&&b.json.data){
        //             that.commentById=b.json.data;
        //             that.commentById1 = that.commentById.list;
        //             if (that.commentById.pageNumber > 1) {
        //                 if (that.commentById.list.length > 0) {
        //                     that.commentById2 = that.commentById2.concat(b.json.data.list);
        //                 }
        //             } else {
        //                 that.commentById2 = that.commentById1;
        //             }
        //             console.log(that.commentById)
        //         }else{
        //             that.commentById = {};
        //             that.commentById1 = [];
        //             that.commentById2 = [];
        //             vm.$refs.prompt.toast("获取商品详情失败！")
        //         }
        //     });
        // },
        getData:function(aaa){
            console.log(aaa.split(" ")[0].split("-"))
        },
        compareDate:function(){
            a=setInterval(this.leftTimer,1000);
            //this.leftTimer();
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            var date = new Date();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            this.startsecond=(hour*60+minute)*60+second;
            if(that.endSecond>that.startsecond){
                that.hours=Math.floor((that.endSecond-that.startsecond)/60/60);
                that.minutes=Math.floor((that.endSecond-that.startsecond)/60%60);
                that.seconds=Math.floor((that.endSecond-that.startsecond)%60);
                that.hours=checkTime(that.hours);
                that.minutes=checkTime(that.minutes);
                that.seconds=checkTime(that.seconds);
            }else{
                clearInterval(a);
            }
        },
        //商品Sku信息
        getProductSkuBySku:function(){
            var that=this;
            this.ajax({url:"/purchase/getProductSkuBySku",data:{skuId:that.skuId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productById.images=b.json.data.images;
                    that.stocks=b.json.data.stocks;
                    that.integral=b.json.data.integral;
                    console.log(that.productById.images)
                    if(that.productById.images){
                        that.imgSrc=that.productById.images[0].imgSrc;
                    }
                }else{
                    vm.$refs.prompt.toast("获取商品Sku信息失败！")
                }
            });
        },
        //选择及时改变
        getInfo:function(type,item){
            var that=this;
            // that.skuId="";
            // that.stocks="";
            // that.integral="";
            if(type==1){
                that.colorName="";
                that.colorId="";
                that.colorName=item.name;
                that.colorId=item.productColorId;
            }else if(type==2){
                that.specName="";
                that.specId="";
                that.specName=item.name;
                that.specId=item.productSpecId;
            }
        },
        //加入购物车
        addCart:function(){
            var that=this;
            if(!userInfo){
                location.href="/loginIndex?number=13&productId="+that.productId;
                return false;
            }else{
                that.uid=userInfo.loginUid;
            }
            if(that.skuId&&that.productById.shopId&&that.number){

            }else{
                vm.$refs.prompt.toast("请选择商品规格数量！");
                vm.$refs.prompt.toastInWechat("请选择商品规格数量！");
                return false;
            }
            var data={
                loginUid:that.uid,
                productId:that.productId,
                skuId:that.skuId,
                shopId:that.productById.shopId,
                number:that.number,
                isBuyNow:that.isBuyNow
            };
            console.log(data)
            this.ajax({url:"/purchase/addCart",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    // that.cartCountNum=b.json.data;
                    // this.isClassify=false;
                    that.isDisplay();
                    if(that.isBuyNow==0){
                       that.getNext(7,b.json.data.cartId)
                    }else{
                        vm.$refs.prompt.toast("加入购物车成功！");
                        vm.$refs.prompt.toastInWechat("加入购物车成功！");
                    }
                    that.skuId="";
                    that.number=1;
                    that.colorId="";
                    that.specId="";
                    that.getProductById();
                }else{
                    if(b.json.msg="用户未登录"){
                        location.href="/loginIndex?number=13&productId="+that.productId;
                    }else{
                        vm.$refs.prompt.toast(b.json.msg||"加入购物车失败！");
                    }
                }
            });
        },
        getNext:function(type,item){
            if(type==1||type==0){
                if(this.skuId&&this.number!=0){
                    this.isBuyNow=type;
                    this.addCart();
                }else{
                    this.isDisplay();
                    //vm.$refs.prompt.toast("请选择规格或数量！");
                }
            }else if(type==3){
                location.href="/purchase/goodsDetails?productId="+item.productId+"&type=1"
            }else if(type==4){
                if(this.productById&&this.productById.coupons.length>0){
                    location.href="/purchase/coupon?productId="+this.productId;
                }else{
                    vm.$refs.prompt.toast("沒有折扣券！");
                }
            }else if(type==5){
                location.href='/purchase/shopIndex?shopId='+this.shop.shopId
            }else if(type==6){
                location.href="/friend?type=1&circleId="+this.productById.userId+"&uid="+this.uid;
            }else if(type==7){
               location.href="/purchase/confirmOrder?uid="+this.uid+"&cartId="+item;
            }else if(type==8){
                location.href="/purchase/shoppingCart";
            }
        },
        getShare:function(){
            this.isShare=!this.isShare;
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=1&postId="+this.productId;
            }else{
                location.href="/friend/forward?number=1&postId="+this.productId;
            }

        },
        getIntegral:function(type){
            var that=this;
            that.skuId="";
            that.stocks="";
            that.integral="";
            that.productById.skus.forEach(function(val){
                if(val.productSpecId==that.specId&&val.productColorId==that.colorId){
                    that.skuId=val.skuId;
                }
            });
            console.log(that.stocks+"-----"+that.integral);
        }
    }
    ,watch:{
        isShow:function(){
            this.$nextTick(function(){
                // if(this.isShow!=3){
                //     sideDeleting();
                // }
                if(this.isShow==1){
                    if(getDocumentTop()>200){
                        vm.isTop=true;
                    }else{
                        vm.isTop=false;
                    }
                    var swiper1 = new Swiper(".purchase-slider .swiper-container",{
                        spaceBetween: 30,
                        centeredSlides: true,
                        // autoplay: {
                        //     delay: 2500,
                        //     disableOnInteraction: false
                        // },
                        pagination: {
                            el: '.swiper-pagination',
                            type: 'fraction'
                        },
                        lazy: {
                            loadPrevNext: true,
                            loadPrevNextAmount: 2
                        },
                        observer: true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents: true//修改swiper的父元素时，自动初始化swiper
                    });
                    var swiper = new Swiper('.goodsDetails-rSlide .swiper-container', {
                        spaceBetween: 10,
                        slidesPerView: 'auto',
                        freeMode: true,
                        lazy: {
                            loadPrevNext: true,
                            loadPrevNextAmount: 4
                        },
                        observer: true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents: true//修改swiper的父元素时，自动初始化swiper
                    });
                    var swiper3 = new Swiper('.secondKill-snatchCent .swiper-container', {
                        slidesPerView: "auto",
                        spaceBetween: 5,
                        loop : true,
                        autoplay:true,
                        observer:true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents:true//修改swiper的父元素时，自动初始化swiper
                    });
                }
                //console.log($(".secondKill-snatchCent .swiper-slide").length);
                //var swiper = new Swiper('.swiper-container');
            });
        },
        num:function(){
            this.$nextTick(function(){
                console.log($(".secondKill-snatchCent .swiper-slide").length);
                var swiper = new Swiper('.secondKill-snatchCent .swiper-container', {
                    slidesPerView: "auto",
                    spaceBetween: 5,
                    loop : true,
                    autoplay:true,
                    observer:true,//修改swiper自己或子元素时，自动初始化swiper
                    observeParents:true//修改swiper的父元素时，自动初始化swiper
                });
            });
        },
        specId:function(){
            this.colorIdList=[];
            this.getIntegral(2);
        },
        colorId:function(){
            this.specIdList=[];
            this.getIntegral(1);
        },
        skuId:function(){
            if(this.skuId){
                this.getProductSkuBySku();
            }
        }
    }
});
var a;
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
//sideDeleting();
function sideDeleting(){
    //侧滑显示删除按钮
    var expansion = null; //是否存在展开的list
    var container = document.getElementById("goodsDetails-Btn");
    // var container = document.querySelectorAll(".goodsDetails-Btn");
    // for(var i = 0; i < container.length; i++){
    //
    // }
    var x, y, X, Y, swipeX, swipeY;
    container.addEventListener("touchstart",function(event) {
        x = event.changedTouches[0].pageX;
        y = event.changedTouches[0].pageY;
        swipeX = true;
        swipeY = true ;
    });
    container.addEventListener("touchmove",function(event){
        X = event.changedTouches[0].pageX;
        Y = event.changedTouches[0].pageY;
        // 左右滑动
        if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
            swipeY = false;
        }
        // 上下滑动
        if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
            // 阻止事件冒泡
            event.stopPropagation();
            // console.log(y-Y);
            vm.swipeY=y-Y;
            if(Y - y > 10){   //上滑
                event.preventDefault();
                vm.isShow=1;
                vm.isTop=false;
                window.scrollTo(0,0);
                // console.log("上滑")
            }
            if(y - Y > 10){   //下滑
                event.preventDefault();
                // console.log("下滑")
                vm.isShow=2;
                window.scrollTo(0,0);
            }
            swipeX = false;
        }
    });
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
var loadFlag;
window.onscroll=function(){
    if(vm.isShow==1){
        if(getDocumentTop()>200){
            vm.isTop=true;
        }else{
            vm.isTop=false;
        }
    }else if(vm.isShow==3){
        // console.log(vm.commentById.lastPage)
        loadFlag = vm.commentById.lastPage;
        // console.log(loadFlag)
        //监听事件内容
        if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
            //当滚动条到底时,这里是触发内容
            if(!loadFlag){
                vm.loadFlag=true;
                vm.pageNumber++;
                console.log("aaaaaaaaaaaaa")
                vm.getProductCommentById();
                // lazyload();
            }else{
                vm.$refs.prompt.toast("没有更多了！")
            }
        }
    }
};